<template>
	<view class="flex-col page">
		<view>
			<view :style="{height:statusBarHeight+'px'}"></view>
			<u-navbar title="收货地址" bgColor="#F5F5F5" @leftClick="$nav(2)">	
			</u-navbar>
		</view>
		
		<view class="content">
			<view class="wrap" v-for="(item, index) in list" :key="item.id">
				<view class="info" @click="handleItemSelect(item)">
					<view class="top u-flex justify-between">
						<view class="u-flex">
							<view class="name">{{item.consignee_name}}</view>
							<view class="ml-20 font-sm">{{item.consignee_mobile}}</view>
						</view>
					</view>
					<view class="bottom">{{item.area_path}}{{item.detail_address}}</view>
				</view>
				<view class="u-flex justify-between">
					<view class="u-flex">
						<u-switch v-model="item.isDefault" size="20" @change="handleDefault(item)"></u-switch>
						<view class="ml-20 font-sm">{{item.isDefault ? '默认地址' : '设为默认'}}</view>
					</view>
					<view class="u-flex">
						<u-button type="primary" size="mini" text="编辑" @click="handleEdit(item)"></u-button>
						<view class="ml-20" @click="handleDelete(item)">
							<u-button type="error" size="mini" text="删除"></u-button>
						</view>
					</view>
				</view>
			</view>
			<u-gap height="200"></u-gap>
			<view class="btn-wrap">
				<u-button type="warning" shape="circle" text="新增收货地址" @click="handleAdd"></u-button>
			</view>
			
			
			
			<u-popup :show="showPop" mode="center" round="10" @close="close">
				<view class="padding">
					<u-form :model="info" ref="uForm" :labelWidth="80">
						<u-form-item label="姓名" prop="info.consignee_name" borderBottom>
							<u-input v-model="info.consignee_name" border="none" placeholder="请填写姓名"></u-input>
						</u-form-item>
						<u-form-item label="手机号" prop="info.consignee_mobile" borderBottom>
							<u-input v-model="info.consignee_mobile" border="none" placeholder="请填写手机号"></u-input>
						</u-form-item>
						<!-- <u-form-item label="设为默认" prop="info.isDefault" borderBottom>
							<u-switch v-model="info.isDefault" size="20"></u-switch>
						</u-form-item> -->
						<u-form-item label="所在地区" prop="info.area_path" borderBottom @click="showPicker = true">
							<u-input v-model="info.area_path" border="none" placeholder="请选择您所在的省市区" readonly suffixIcon="arrow-right" suffixIconStyle="color: #909399"></u-input>
						</u-form-item>
						<u-form-item label="详细地址" prop="info.detail_address" borderBottom>
							<u-input v-model="info.detail_address" border="none" placeholder="请输入详细地址" ></u-input>
						</u-form-item>
					</u-form>
					<view class="form-wrap">
						<u-button type="primary" shape="circle" text="确定" @click="handleSubmit"></u-button>
					</view>
				</view>
			</u-popup>
			<wmCityselect v-model="showPicker" @city-change="handleCityChange"></wmCityselect>
		</view>
	</view>
</template>

<script>
	import wmCityselect from '@/components/cityselect.vue';
	export default {
		data() {
			return {
				list:[],
				showPop: false,
				showPicker: false,
				info: {}
			};
		},
		components: {
			wmCityselect
		},
		onLoad() {
			this.getAddressList();
		},
		computed:{
			statusBarHeight(){
				return this.$store.state.statusBarHeight +44;
			}
		},
		methods:{
			getAddressList(){
				this.$request({
					url:'?c=User&a=addressList',
					type:'post',
				}).then(res =>{
					this.list = res.data;
					if(this.list){
						this.list.forEach((item, index, array) => {
						   if(item.is_default == 0){
							   item.isDefault = false;
						   }else{
							   item.isDefault = true;
						   }
						});	
					}
					
				})
			},
		
			close() {
				this.showPop = false;
			},
			
			handleAdd() {
				this.info = {}
				this.showPop = true
			},
			handleItemSelect(item) {
				uni.$emit('isRefresh', item)
				uni.navigateBack({
					delta: 1
				})
			},
			handleDefault(item) {
				let code = 0;
				if(item.isDefault){
					code = 1;
				}else{
					code = 0;
				}
				this.$request({
					url:'?c=User&a=addressEdit',
					type:'post',
					data:{
						id:item.id,
						consignee_name:item.consignee_name,
						consignee_mobile:item.consignee_mobile,
						area_path:item.area_path,
						detail_address:item.detail_address,
						is_default:code,
						region_code:item.region_code
					}
				}).then(res =>{
					this.$u.toast('成功');
					this.showPop = false;
					this.getAddressList();
				})
			},
			handleEdit(item) {
				this.info = item
				this.info.area_path = item.area_path
				this.showPop = true
			},
			handleDelete(item) {
				let that = this;
				uni.showModal({
					title:  '提示',
					content: '确认删除该地址吗？',
					cancelText: '取消',
					confirmText: '确定',
					confirmColor: '#007aff',
					success(res) {
						if (res.confirm) {
							that.addressDelete(item);
						}
					}
				})
			},
			
			addressDelete(item){
				this.$request({
					url:'?c=User&a=addressDel',
					type:'post',
					data:{
						id:item.id
					}
				}).then(res =>{
					this.getAddressList();
				});
			},
			handleCityChange(e) {
				this.info.area_path = e.province.label + e.city.label + e.area.label;
				this.info.region_code = e.area.value;
				
			},
			// 添加或编辑地址（data中有id即为编辑，否则为添加）
			handleSubmit() {
				if(this.info.id){ // 编辑
					this.$request({
						url:'?c=User&a=addressEdit',
						type:'post',
						data:{
							id:this.info.id,
							consignee_name:this.info.consignee_name,
							consignee_mobile:this.info.consignee_mobile,
							area_path:this.info.area_path,
							detail_address:this.info.detail_address,
							is_default:this.info.is_default,
							region_code:this.info.region_code
						}
					}).then(res =>{
						this.$u.toast('成功');
						this.showPop = false;
						this.getAddressList();
					})
					
				}else{
					if (!this.info.consignee_name) return this.$u.toast("请输入收货人姓名");
					if (!this.info.consignee_mobile) return this.$u.toast("请输入收货人手机");
					if (!this.info.area_path) return this.$u.toast("请选择地址");
					if (!this.info.detail_address) return this.$u.toast("请输入详细地址");
					this.$request({
						url:'?c=User&a=addressAdd',
						type:'post',
						data:{
							consignee_name:this.info.consignee_name,
							consignee_mobile:this.info.consignee_mobile,
							area_path:this.info.area_path,
							detail_address:this.info.detail_address,
							region_code:this.info.region_code
						}
					}).then(res =>{
						this.$u.toast('成功');
						this.showPop = false;
						this.getAddressList();
					})
				}
			}
			
			
		}
	}
</script>

<style lang="scss">
.page{
	width: 100%;
	height: 100vh;
	overflow-y: auto;
	overflow-x: hidden;
}
.u-flex {
		display: flex;
		align-items: center;
	}
	
	.justify-between {
		justify-content: space-between;
	}

	.ml-20 {
		margin-left: 20rpx;
	}

	.font-sm {
		font-size: 24rpx;
		color: #666;
	}
	.padding {
		padding: 30rpx;
	}
	.content {
		padding: 30rpx;
		background: #f5f6f8;
		width: 100vw;
		min-height: 100vh;
		box-sizing: border-box;

		.wrap {
			background: #fff;
			border-radius: 20rpx;
			padding: 20rpx;
			margin-bottom: 20rpx;

			.top {
				.name {
					font-weight: bold;
				}
			}

			.bottom {
				margin: 20rpx 0;
				color: #999;
				font-size: 28rpx;
				padding-bottom: 20rpx;
				border-bottom: 1px solid #eee;
			}

		}

		.btn-wrap {
			position: fixed;
			bottom: 40rpx;
			left: 100rpx;
			z-index: 999;
			text-align: center;
			width: 550rpx;
		}
	}
	.form-wrap {
		margin-top: 40rpx;
	}
</style>
